package com.shanghaionstar.smart;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.smart.iview.ISmartAddDeviceView;
import com.shanghaionstar.smart.presenter.SmartAddDevicePresenter;
import com.shanghaionstar.smart.util.NetUtil;

/**
 * Created by Rengui Lu on 2016/6/12.
 */
public class SmartAddDeviceFrg extends BaseFragment implements View.OnClickListener,ISmartAddDeviceView {
    @FindViewById(R.id.title_tv)
    TextView mTitleTv;
    @FindViewById(R.id.sub_title_tv)
    TextView mSubTitleTv;
    @FindViewById(R.id.back_ib)
    ImageButton mBackIb;
    @FindViewById(R.id.disconnect_rl)
    RelativeLayout mDisconnectRl;
    @FindViewById(R.id.ssid_rl)
    RelativeLayout mSsidRl;
    @FindViewById(R.id.wifi_pw_et)
    EditText mWifiPwEt;
    @FindViewById(R.id.pw_iv)
    ImageView mPwIv;
    @FindViewById(R.id.next_btn)
    Button mNextBtn;
    @FindViewById(R.id.scan_wifi_pl)
    ProgressLayout mDeviceWifiPl;
    @FindViewById(R.id.connect_failed_tv)
    TextView failedTv;

    private TextView mSsidTv;

    private boolean isHiddenPw = true;

    private BaseActivity mActivity;
    private SmartAddDevicePresenter presenter;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (BaseActivity) activity;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        if(isFirstLoad()){
            presenter = new SmartAddDevicePresenter(mActivity.getScheduler());
            presenter.setView(this);
        }
    }

    private void initView() {
        mTitleTv.setText(R.string.smart_add_device);
        mSsidTv = (TextView) mSsidRl.findViewById(R.id.title_tv);
        mSubTitleTv.setText(R.string.smart_fun_intro);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (NetUtil.isWifiConnected(mActivity)) {
            mDisconnectRl.setVisibility(View.GONE);
            mSsidTv.setText(NetUtil.getCurrentSSID(mActivity) == null ? "" : NetUtil.getCurrentSSID(mActivity));
        } else {
            mDisconnectRl.setVisibility(View.VISIBLE);
            mSsidTv.setText("");
            mWifiPwEt.setText("");
        }

        if(mActivity instanceof  SmartHomeActivity){
            if(((SmartHomeActivity) mActivity).hasFailed) {
//                failedTv.setVisibility(View.VISIBLE);
                showFailedDia();
                ((SmartHomeActivity) mActivity).hasFailed = false;
            }else{
                failedTv.setVisibility(View.GONE);
            }
        }

    }

    private void showFailedDia(){
        new AlertDialog.Builder(mActivity)
                .setMessage(getString(R.string.smart_bind_failed))
                .setPositiveButton(R.string.smart_try_again, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                      goToCapture();
                    }
                })
                .setNegativeButton(R.string.smart_cancel,null)
                .show();
    }

    private void goToCapture(){
        String routerSsid = mSsidTv.getText().toString();
        String routerPw = mWifiPwEt.getText().toString();
        presenter.goToCapture(routerSsid,routerPw);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_slk_add_device;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * @param et
     * @param iv
     * @param isHidden
     * 眼睛点击隐藏和显示
     */
    private void showOrHiddenPw(EditText et, ImageView iv, boolean isHidden) {
        if (isHidden) {
            et.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            iv.setImageResource(R.drawable.smart_show_pw);
        } else {
            et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            iv.setImageResource(R.drawable.smart_hide_pw);
        }
       isHiddenPw = !isHidden;
        et.postInvalidate();
    }


    @OnClick({R.id.next_btn, R.id.disconnect_rl, R.id.pw_iv, R.id.ssid_rl, R.id.back_ib, R.id.sub_title_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.next_btn:
                goToCapture();
                break;
            case R.id.sub_title_tv:
                presenter.goToFunIntro();
                break;
            case R.id.disconnect_rl:
                presenter.goToWifiSetting();
                break;
            case R.id.pw_iv:
                showOrHiddenPw(mWifiPwEt, mPwIv, isHiddenPw);
                break;
            case R.id.ssid_rl:
                presenter.goToWifiSetting();
                break;
            case R.id.back_ib:
                back();
                break;
        }
    }

    public void goToConnect(String mQRcode){
        String routerSsid = mSsidTv.getText().toString();
        String routerPw = mWifiPwEt.getText().toString();
        presenter.goToConnect(mQRcode,routerSsid,routerPw);
    }


    @Override
    public void setLoading(boolean isLoading) {

    }

    @Override
    public void onResponseError(OnStarException e) {

    }

    @Override
    public void onReponseSuccess(Object object) {

    }

    @Override
    public Activity onActivity() {
        return mActivity;
    }
}
